extends Node
enum State{
	closed,
	connecting,
	connected
}
var websocket_url = "ws://localhost:3000"
var _client=WebSocketClient.new()
var _dataArr=[]
var state=State.closed
func start():
	_client.connect("connection_closed", self, "_closed")
	_client.connect("connection_error", self, "_closed")
	_client.connect("connection_established", self, "_connected")
	_client.connect("data_received", self, "_on_data")
	set_process(false)
	_startConnect()
func _startConnect():
	state=State.connecting
	var err = _client.connect_to_url(websocket_url)
	if err != OK:
		print("Unable to connect")
		set_process(false)
		state=State.closed
	pass
func _closed(was_clean = false):
	print("Closed, clean: ", was_clean)
	set_process(false)
	state=State.closed
func _connected(proto = ""):
	state=State.connected
	print("Connected with protocol: ", proto)
	_client.get_peer(1).put_packet("Test packet".to_utf8())
func _on_data():
	var packet=_client.get_peer(1).get_packet()
	var data=packet.get_string_from_utf8()
	_dataArr.append(data)
func _process(delta):
	_client.poll()
	#从队列里拿出消息
	while true:
		var data=_dataArr.pop_front()
		if !data:
			break;
		var obj=parse_json(data)
		if !obj:
			break;
		_dealMessage(obj)
		
	if Input.is_action_just_pressed("websocket_connect"):
		if state==State.closed:
			_startConnect()
		pass
func _dealMessage(message):
	if !message:
		return 
	var cmd=message.get('cmd','')
	match cmd:
		'INTERACT_WORD': #加入直播间
			var data=message.get('data',{})
			var uid=str(data.get('uid',''))
			var uname=data.get('uname','')
			if !uid:
				return
			PlayerManager.addPlayer(uid,uname)
		'ENTRY_EFFECT': #进场特效，比如 欢迎舰长xxx进入直播间。如果有这个，就没有INTERACT_WORD。
			pass
		'DANMU_MSG': #弹幕
			var info=message.get('info',[])
			var txt=F.arrGet(info,1,'') as String
			var arr=F.arrGet(info,2,[])
			var uid=F.arrGet(arr,0,'')
			var uname=F.arrGet(arr,1,'')
			if !uid:
				return
			var player=PlayerManager.addPlayer(uid,uname) as Player
			#播放对话
			var actor=player.actor as Actor
			if !actor:
				return
			actor.showDialogue(txt)
			#解析弹幕内容
			if txt=='换皮肤' or txt=='切换':
				actor.randomSkin()
			elif txt=='两只老虎':
				actor.setSkin(10)
				pass
			elif txt=='向左走' or txt=='向左' or txt=='向左跑':
				actor.goLeft()
				pass
			elif txt=='向上走' or txt=='向后走' or txt=='向后' or txt=='向上' or txt=='向上跑' or txt=='向后跑':
				actor.goUp()
				pass
			elif txt=='向下走' or txt=='向下' or txt=='向前' or txt=='向前走' or txt=='向下跑' or txt=='向前跑':
				actor.goDown()
				pass
			elif txt=='向右走' or txt=='向右' or txt=='向右跑':
				actor.goRight()
				pass
			elif txt=='去中间' or txt=='中间':
				actor.gotoCenter()
				pass
			elif txt=='站立' or txt=='停' or txt=='停止':
				actor.stopMove()
				pass
			elif txt.begins_with('一起喊') && player.djState==1:
				txt.erase(0,3)
				if txt.begins_with(' '):
					txt.erase(0,1)
				for p in PlayerManager.activePlayerArr:
					if p==player:
						continue
					var ac=p.actor as Actor
					if ac:
						ac.showDialogue(txt)
			elif txt=='走位' or txt=='疯狂走位' or txt=='随机走位':
				actor.startRandomMove()
				pass
			elif txt=='切歌' and player.djState==1:
				SoundManger.nextSound()
			elif txt=='变大':
				actor.big+=0.1
			elif txt=='变小':
				actor.big-=0.1
			elif txt=='旋转':
				actor.isRotating=true
			elif txt=='停止旋转':
				actor.isRotating=false
		'SEND_GIFT': #投喂
			var data=message.get('data',{})
			var uid=data.get('uid','')
			var uname=data.get('uname',"")
			if !uid:
				return
			var player=PlayerManager.addPlayer(uid,uname)
			var txt=data.get('giftName',"")
			var actor=player.actor as Actor
			if !actor:
				return
			actor.showDialogue(txt)
			#特写
			var cameraCtl=G.cameraCtl as CameraCtl
			cameraCtl.teXie(actor)
			pass
		'ONLINE_RANK_COUNT': #自己的排名，没用
			
			pass
		'WATCHED_CHANGE':  #多少人看过直播间
#			var data=message.get('data',{})
#			var num=data.get('num',0)
			pass
		'ONLINE_RANK_V2': #高能榜
			var data=message.get('data',{})
			var list=data.get('list',[])
			var arr=[] #uid 的arr
			for item in list:
				var uid=item.get('uid','')
				arr.append(uid)
			arr=arr.slice(0,2)
			DjManager.onRankChangeUid(arr)
			pass
	pass
